﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UITournament.FactoryMethod;

namespace UITournament
{
    public class Athlete : Person
    {
        private int uniqueNumber;
        private DateTime dateOfBirth;
        private Gender gender;
        private Trainer trainer;

        public Trainer Trainer
        {
            get { return trainer; }
            set { trainer = value; }
        }


        public Gender Gender
        {
            get { return gender; }
            set { gender = value; }
        }


        public DateTime DateOfBirth
        {
            get { return dateOfBirth; }
            set { dateOfBirth = value; }
        }


        public int UniqueNumber
        {
            get { return uniqueNumber; }
            set
            {            
                try
                {
                    if (Validation.ValidateNumber(value.ToString()))
                    {
                        uniqueNumber = value;
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidUniqueNumberException("The entered value is not valid! Allowed values: 0..9", ex);
                }
            }
        }

        public Athlete(string firstName, string lastName, Gender gender)
            : base(firstName, lastName)
        {
            this.Gender = gender;
        }

        public Athlete(string firstName,
                        string lastName,
                        Gender gender,
                        int uniqueNumber,
                        DateTime dateOfBirth,
                        Team team)
            : this(firstName, lastName, gender)
        {
            this.UniqueNumber = uniqueNumber;
            this.DateOfBirth = dateOfBirth;
            this.Trainer.Team = team;
        }

        public override string ToString()
        {
            StringBuilder result = new StringBuilder();
            result.Append("Athlete: " + this.FirstName + " " + this.LastName + "\n");
            result.Append("Gender: " + this.Gender + "\n");

            if (!(this.DateOfBirth == null || this.Trainer == null || this.Trainer.Team == null))
            {
                result.Append("Year of birth: " + this.DateOfBirth.Year + "\n");
                //result.AppendFormat("{0} years",(DateTime.Now.Year - this.DateOfBirth.Year).ToString()); just testing
                result.Append(this.Trainer.Team + "\n");
                result.Append(this.Trainer);
            }

            return result.ToString();
        }

        public override void Create()
        {
            //TODO
            //Add athlete to List of Athlets
        }

    }
}
